﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using _12306Bot.Common;

namespace _12306Bot
{
    public partial class FrmSetup : Form
    {
        private FrmMain frmParent;

        public FrmSetup()
        {
            InitializeComponent();
        }

        public FrmSetup(FrmMain frmPara)
            : this()
        {
            this.frmParent = frmPara;
        }

        private void FrmSetup_Load(object sender, EventArgs e)
        {
            System.Configuration.Configuration cfg = System.Configuration.ConfigurationManager.OpenExeConfiguration(System.Configuration.ConfigurationUserLevel.None);
            String strProxy = DES.DecryptDES(cfg.AppSettings.Settings["Proxy"].Value, DES.strDESKey);
            txtProxyAddress.Text = strProxy.Split('|')[0];
            txtPort.Text = strProxy.Split('|')[1];
            txtUserName.Text = strProxy.Split('|')[2];
            txtPassWord.Text = strProxy.Split('|')[3];

            if (cfg.AppSettings.Settings["UseDefaultProxy"].Value == "Y")
            {
                cbUseDefaultProxy.Checked = true;
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            String strErrMsg = "";

            Regex rgIP = new Regex(@"^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$");
            if (txtProxyAddress.Text.Trim() != "" && !rgIP.Match(txtProxyAddress.Text).Success)
            {
                strErrMsg += "IP地址格式不对。\r\n";
            }

            Regex rgNum = new Regex(@"^[0-9]*$");
            if (!rgNum.IsMatch(txtPort.Text))
            {
                strErrMsg += "端口应该为整数。\r\n";
            }

            if (strErrMsg != "")
            {
                MessageBox.Show(strErrMsg, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            System.Configuration.Configuration cfg = System.Configuration.ConfigurationManager.OpenExeConfiguration(System.Configuration.ConfigurationUserLevel.None);

            cfg.AppSettings.Settings["Proxy"].Value = DES.EncryptDES(txtProxyAddress.Text + "|" + txtPort.Text + "|" + txtUserName.Text + "|" + txtPassWord.Text, DES.strDESKey);
            if (cbUseDefaultProxy.Checked)
            {
                cfg.AppSettings.Settings["UseDefaultProxy"].Value = "Y";
            }
            else
            {
                cfg.AppSettings.Settings["UseDefaultProxy"].Value = "N";
            }
            cfg.Save();
            //写完信息后让主窗体读取
            frmParent.setWebBot();
            //初始化登陆信息
            frmParent.initLogin();

            this.Dispose();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void cbUseDefaultProxy_CheckedChanged(object sender, EventArgs e)
        {
            System.Configuration.Configuration cfg = System.Configuration.ConfigurationManager.OpenExeConfiguration(System.Configuration.ConfigurationUserLevel.None);
            //cfg.

        }
    }
}
